package br.com.virtualsoluction.gui;

import br.com.virtualsoluction.bo.Produto;
import javax.swing.JOptionPane;

public class FormCadProduto extends javax.swing.JFrame {

    Produto p;
    String op = "";

    public FormCadProduto() {
        initComponents();

        p = new Produto();
        p.getListaProdutos();        
        statusCampos(false);
        statusBotoes(0);
        carregaCampos(p);         
    }

    public final void carregaCampos(Produto p){
        txtCodigo.setText(String.valueOf(p.getCodigo()));
        txtDescricao.setText(p.getDescricao());
        txtEstoque.setText(String.valueOf(p.getEstoque()));
        txtPrecoVenda.setText(String.valueOf(p.getPrecoVenda()));        
    }

    public final void limpaCampos(){
        txtCodigo.setText("");
        txtDescricao.setText("");
    }
    
    public final void statusBotoes(int s){
        // 0 Visualização
        // 1 Edicao

        btnSalvar.setEnabled(s==1);
        btnEditar.setEnabled(s==0);
        btnExcluir.setEnabled(s==0);
        btnNovo.setEnabled(s==0);
    }
    
    public final void statusCampos(boolean v){
        txtCodigo.setEditable(v);
        txtDescricao.setEditable(v);
        txtEstoque.setEditable(v);
        txtPrecoVenda.setEditable(v);
    }

    public final void statusTela(int s){
        // 0 Visualização
        // 1 Edição
        // 2 Inclusão

        txtCodigo.setEditable(s == -1);
        txtDescricao.setEditable((s == 1 || s == 2));
        txtEstoque.setEditable((s == 1 || s == 2));
        txtPrecoVenda.setEditable((s == 1 || s == 2));
        btnSalvar.setEnabled((s == 1 || s == 2));
        btnEditar.setEnabled(s==0);
        btnExcluir.setEnabled(s==0);
        btnNovo.setEnabled(s==0);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnInserirMarca = new javax.swing.JButton();
        btnInserirCategoria1 = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        btnNovo = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnAnterior = new javax.swing.JButton();
        btnProximo = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnSair1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtDescricao = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtPrecoVenda = new javax.swing.JTextField();
        txtEstoque = new javax.swing.JTextField();

        btnInserirMarca.setText("...");
        btnInserirMarca.setToolTipText("");
        btnInserirMarca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInserirMarcaActionPerformed(evt);
            }
        });

        btnInserirCategoria1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Configuração do banco de dados");
        setAlwaysOnTop(true);
        setIconImages(null);
        setMinimumSize(new java.awt.Dimension(150, 130));

        jToolBar1.setBorder(null);
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnNovo.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/document_new_lined_24_h.png"))); // NOI18N
        btnNovo.setText("Novo");
        btnNovo.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnNovo.setFocusable(false);
        btnNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNovo.setMaximumSize(new java.awt.Dimension(70, 50));
        btnNovo.setMinimumSize(new java.awt.Dimension(50, 50));
        btnNovo.setOpaque(false);
        btnNovo.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/document_new_lined_24_h.png"))); // NOI18N
        btnNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNovo);

        btnSalvar.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/save_24_h.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnSalvar.setFocusable(false);
        btnSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSalvar.setMaximumSize(new java.awt.Dimension(70, 50));
        btnSalvar.setMinimumSize(new java.awt.Dimension(50, 50));
        btnSalvar.setName("btnSalvar"); // NOI18N
        btnSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSalvar);

        btnEditar.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/document_new_unlined_24_h.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnEditar.setFocusable(false);
        btnEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEditar.setMaximumSize(new java.awt.Dimension(70, 50));
        btnEditar.setMinimumSize(new java.awt.Dimension(50, 50));
        btnEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEditar);

        btnExcluir.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/delete_24_h.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnExcluir.setFocusable(false);
        btnExcluir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnExcluir.setMaximumSize(new java.awt.Dimension(70, 50));
        btnExcluir.setMinimumSize(new java.awt.Dimension(50, 50));
        btnExcluir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });
        jToolBar1.add(btnExcluir);
        jToolBar1.add(jSeparator1);

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/arrow_left_24_h.png"))); // NOI18N
        btnAnterior.setText("Anterior");
        btnAnterior.setFocusable(false);
        btnAnterior.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAnterior.setMaximumSize(new java.awt.Dimension(70, 50));
        btnAnterior.setMinimumSize(new java.awt.Dimension(50, 50));
        btnAnterior.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });
        jToolBar1.add(btnAnterior);

        btnProximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/arrow_right_24_h.png"))); // NOI18N
        btnProximo.setText("Próximo");
        btnProximo.setFocusable(false);
        btnProximo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnProximo.setMaximumSize(new java.awt.Dimension(70, 50));
        btnProximo.setMinimumSize(new java.awt.Dimension(50, 50));
        btnProximo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProximoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnProximo);
        jToolBar1.add(jSeparator2);

        btnSair1.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnSair1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/virtualsoluction/imagens/go_arrow_round_24_h.png"))); // NOI18N
        btnSair1.setText("Sair");
        btnSair1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnSair1.setFocusable(false);
        btnSair1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSair1.setMaximumSize(new java.awt.Dimension(70, 50));
        btnSair1.setMinimumSize(new java.awt.Dimension(50, 50));
        btnSair1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSair1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSair1ActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSair1);

        jPanel1.setBackground(new java.awt.Color(51, 51, 255));
        jPanel1.setForeground(new java.awt.Color(102, 102, 255));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Cadastro de produto");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 505, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 188, Short.MAX_VALUE)
                    .addComponent(jLabel6)
                    .addGap(0, 188, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 7, Short.MAX_VALUE)
                    .addComponent(jLabel6)
                    .addGap(0, 8, Short.MAX_VALUE)))
        );

        jLabel1.setText("Código......:");

        jLabel2.setText("Descrição:");

        txtDescricao.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDescricaoFocusLost(evt);
            }
        });

        jLabel3.setText("Estoque.:");

        jLabel4.setText("Preço de venda:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addGap(10, 10, 10)
                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel3)
                .addGap(22, 22, 22)
                .addComponent(txtEstoque, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPrecoVenda, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16))
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(txtEstoque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtPrecoVenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-521)/2, (screenSize.height-245)/2, 521, 245);
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        int ret;
        String m = "Deseja editar o cadastro do produto?";
        ret = JOptionPane.showConfirmDialog (jLabel6, m, "Confirmar edição",1);
       
        if(ret == 0){
            statusBotoes(1);
            statusCampos(true);
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        if (op.equals("I")) {

            p.setDescricao(txtDescricao.getText());
            p.setPrecoVenda(Double.valueOf(txtPrecoVenda.getText()));
            p.setEstoque(Integer.valueOf(txtEstoque.getText()));

            if (p.inserir()) {
                JOptionPane.showMessageDialog(txtDescricao, "Produto '" + p.getDescricao() + "' cadastrado com sucesso");
                carregaCampos(p);
            }
        } else if (op.equals("E")) {
            p.setDescricao(txtDescricao.getText());
            if (p.atualizar()) {
                JOptionPane.showMessageDialog(txtDescricao, "Produto '" + p.getDescricao() + "' foi atualizado com sucesso!");
                carregaCampos(p);
            }
        }
        statusTela(0);
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        op = "I";
        limpaCampos();
        statusTela(2);        
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnSair1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSair1ActionPerformed
         this.dispose();
    }//GEN-LAST:event_btnSair1ActionPerformed

    private void btnInserirMarcaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInserirMarcaActionPerformed
       
    }//GEN-LAST:event_btnInserirMarcaActionPerformed

    private void txtDescricaoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDescricaoFocusLost
        if(txtDescricao.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Campo Vazio", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_txtDescricaoFocusLost

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        p.anterior();
        carregaCampos(p);
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProximoActionPerformed
        p.proximo();
        carregaCampos(p);
    }//GEN-LAST:event_btnProximoActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnExcluirActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormCadProduto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnInserirCategoria1;
    private javax.swing.JButton btnInserirMarca;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnProximo;
    private javax.swing.JButton btnSair1;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescricao;
    private javax.swing.JTextField txtEstoque;
    private javax.swing.JTextField txtPrecoVenda;
    // End of variables declaration//GEN-END:variables

}
